# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=heimdal
pkgname=('heimdal' 'heimdal-libs' 'heimdal-devel')
pkgver=7.7.0
pkgrel=1
pkgdesc="Implementation of Kerberos V5 libraries"
arch=('i686' 'x86_64')
url="https://www.h5l.org/"
license=('custom')
makedepends=('libdb-devel' 'libcrypt-devel' 'libedit-devel' 'libsqlite-devel' 'openssl-devel' 'perl-JSON' 'python2') #libldap-devel
options=('!libtool' '!emptydirs' 'staticlibs')
groups=('net-utils')
source=(https://github.com/heimdal/heimdal/archive/heimdal-${pkgver}.tar.gz
        1.2.1-test-modules.patch
        1.5.2-hdbdir.patch
        1.5.2-install-catman.patch
        1.5.2-roken-signal.patch
        1.5.3-missing-libs-pkg-config.patch
        1.5.3-fix-detect-libedit.patch
        7.5.0-use-python2.patch
        7.5.0-hcrypto-build-fix.patch
        7.5.0-names-clash-with-openssl.patch)
sha256sums=('f7d414d0914abb0e151a276b4de22cf4977fd6c28bd9ecdd990407b1138a945c'
            'b18c9ca3f2db08cea0a4b7d7f07e8656360af848b3a30a5fa8d748240989da49'
            'a99f0f5b69d32524399b1343a4f08b2de43351828b5b45a938f72bacb0eb9085'
            'd68bb0f8f01cc748a6ef63c74a4a15c75553c2322e9bd2649c54cb23077d521b'
            'bf19c5d7326a0cf58a5b4922a9c96fb78807226bc40c2248417024b961dd559e'
            'eecd77ec817f88406f315c1b21559d888f1e0b3182aad317031f7ebc62f69b5b'
            'bb96587539084c2ea9e06da8d2b4c768edd8dd422587a045ea60d1aa3eeb6a8a'
            '59186e1a69c927e7c4c88423a726ed2592b39ec98f57ecd84ec96f12330f4743'
            'f387721cc311a82ce2d4793187957d5323aa773538ea3268175741712534f8d4'
            'bfe321c0391d6f7e23abecc22e9941bcb79d0b67abb9975246a9f462ec32513b')

prepare() {
  cd ${srcdir}/heimdal-heimdal-${pkgver}

  patch -p2 -i "${srcdir}"/1.2.1-test-modules.patch
  patch -p2 -i "${srcdir}"/1.5.2-hdbdir.patch
  patch -p2 -i "${srcdir}"/1.5.2-install-catman.patch
  patch -p2 -i "${srcdir}"/1.5.2-roken-signal.patch
  patch -p1 -i "${srcdir}"/1.5.3-missing-libs-pkg-config.patch
  patch -p1 -i "${srcdir}"/1.5.3-fix-detect-libedit.patch
  patch -p1 -i "${srcdir}"/7.5.0-use-python2.patch
  patch -p1 -i "${srcdir}"/7.5.0-hcrypto-build-fix.patch
  patch -p1 -i "${srcdir}"/7.5.0-names-clash-with-openssl.patch

  ./autogen.sh
}

build() {
  cd ${srcdir}/heimdal-heimdal-${pkgver}

  ./configure \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/libexec \
    --sbindir=/usr/bin \
    --sysconfdir=/etc/krb5 \
    --mandir=/usr/share/man \
    --datadir=/var/lib/heimdal \
    --localstatedir=/var/lib/heimdal \
    --with-openssl=/usr \
    --with-libedit=/usr \
    --with-sqlite3=/usr \
    --enable-shared=yes \
    --enable-static \
    --without-x \
    --disable-kcm \
    --disable-ndbm-db \
    --disable-heimdal-documentation \
    ac_cv_header_winsock2_h=no \
    ac_cv_header_ws2tcpip_h=no
  
  make LDFLAGS="${LDFLAGS} -no-undefined"
  setup
}

setup() {
  local destdir=${srcdir}/dest
  cd ${srcdir}/heimdal-heimdal-${pkgver}
  make DESTDIR=${destdir} install

  # Remove daemons and their manpages
  for i in telnetd ftpd rshd; do
    if [ -f ${destdir}/usr/share/man/man8/${i}.8 ]; then
      rm ${destdir}/usr/share/man/man8/${i}.8
    fi
    if [ -f ${destdir}/usr/libexec/${i} ]; then
      rm ${destdir}/usr/libexec/${i}
    fi
  done

  # Rename clients and their manpages
  for i in rcp rsh telnet ftp su login; do
    if [ -f ${destdir}/usr/share/man/man1/${i}.1 ]; then
      mv ${destdir}/usr/share/man/man1/{,k}${i}.1
    fi
    if [ -f ${destdir}/usr/bin/${i} ]; then
      mv ${destdir}/usr/bin/{,k}${i}
    fi
  done

  # install config
  # Do not install the config as this will conflict with krb5
  install -D -m644 ${srcdir}/heimdal-heimdal-${pkgver}/krb5.conf ${destdir}/etc/krb5/krb5.conf

  # Install the license
  install -Dm644 ${srcdir}/heimdal-heimdal-${pkgver}/LICENSE \
    ${destdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_heimdal() {
  groups=('sys-utils')
  depends=('heimdal-libs')
  backup=(etc/krb5/krb5.conf)

  mkdir -p ${pkgdir}/usr

  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  rm -f ${pkgdir}/usr/bin/*.dll
  cp -rf ${srcdir}/dest/etc ${pkgdir}/
  cp -rf ${srcdir}/dest/usr/libexec ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
}

package_heimdal-libs() {
  depends=('libdb' 'libcrypt' 'libedit' 'libsqlite' 'libopenssl')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_heimdal-devel() {
  pkgdesc="Heimdal headers and libraries"
  groups=('development')
  depends=('heimdal-libs' 'libcrypt-devel' 'libedit-devel' 'libdb-devel' 'libsqlite-devel')

  mkdir -p ${pkgdir}/usr

  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/

  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
}
